package YottaTron.RobotEditor;

import YottaTron.GraphicsLoader;
import YottaTron.Runner;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;


public class EditorPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener{
    static final int BODY = 0;
    static final int GEAR = 1;
    static final int WEAPONS = 2;
    static final int ENGINE = 3;

    boolean[] menu = new boolean[4];
    int width = 800;
    int height = 600;
    Color backgroundColor = Color.BLACK;
    GraphicsLoader backgroundImage;
    Insets insets;
    JButton bBody, bGear, bWeapons, bEngine;

    public EditorPanel(){
        this.setLayout(null);
        insets = this.getInsets();
        try {
            backgroundImage = new GraphicsLoader(Runner.graphicsDir + "robotEditorBackground.png");
        } catch (IOException ex) {
            Logger.getLogger(EditorPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        bBody = createButton("icons/bBody", 249, 49);
        bGear = createButton("icons/bGear", 358, 49);
        bWeapons = createButton("icons/bWeapons", 466, 49);
        bEngine = createButton("icons/bEngine", 617, 50);

        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        this.setPreferredSize(new Dimension(width, height));

        repaint();
    }

    JButton createButton(String fileName, int x, int y){
        JButton b = new JButton();
        b.setBorderPainted(false);
        b.setFocusPainted(false);
        b.setContentAreaFilled(false);
        b.setSelectedIcon(new ImageIcon(Runner.graphicsDir + fileName + "On.png"));
        b.setPressedIcon(new ImageIcon(Runner.graphicsDir + fileName + "On.png"));
        b.setIcon(new ImageIcon(Runner.graphicsDir + fileName + "Off.png"));
        b.setDisabledIcon(new ImageIcon(Runner.graphicsDir + fileName + "Off.png"));
        b.setRolloverIcon(new ImageIcon(Runner.graphicsDir + fileName + "Over.png"));
        b.setRolloverSelectedIcon(new ImageIcon(Runner.graphicsDir + fileName + "Over.png"));
        b.setBounds(x, y, b.getIcon().getIconWidth(), b.getIcon().getIconHeight());
        b.addActionListener(this);
        this.add(b);
        return b;
    }


    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        
//        g2.setColor(Color.BLACK);
//        g2.fillRect(0, 0, width, height);

        g2.drawImage(backgroundImage.getImage(0), 0, 0, null);

//        g2.setColor(Color.lightGray);
//        g2.drawRect(width - 200, 10, 190, 190);
    }

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton) e.getSource();
        //b.setSelected(!b.isSelected());
        if (b.equals(bBody)){
            menu[BODY] = true;
            menu[GEAR] = false;
            menu[WEAPONS] = false;
            menu[ENGINE] = false;
        }
        if (b.equals(bGear)){
            menu[BODY] = false;
            menu[GEAR] = true;
            menu[WEAPONS] = false;
            menu[ENGINE] = false;
        }
        if (b.equals(bWeapons)){
            menu[BODY] = false;
            menu[GEAR] = false;
            menu[WEAPONS] = true;
            menu[ENGINE] = false;
        }
        if (b.equals(bEngine)){
            menu[BODY] = false;
            menu[GEAR] = false;
            menu[WEAPONS] = false;
            menu[ENGINE] = true;
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }



}
